<?php
namespace app\common\model;

class BonusMemberTotal extends Base
{

	/**
	 * 累计某会员某某奖金总数
	 * @param  [type] $uid        [description]
	 * @param  [type] $bonus_name [description]
	 * @param  [type] $inc_amount [description]
	 * @return [type]             [description]
	 */
	public function incMemberBonus($uid,$bonus_name,$inc_amount){
		// 如果该会员该奖金存在记录，累加，否则插入
		if($record = $this->getOneData($bonus_name,$uid)){
			$this->where('id',$record['id'])->setInc('total',$inc_amount);
		}else{
			$this->save([
				'uid'=>$uid,
				'bonus_name'=>$bonus_name,
				'total'=>$inc_amount
			]);
		}
		return true;
	}
	/**
	 * 查询某个会员某个奖金总额记录
	 * @param  [type] $bonus_name [description]
	 * @param  [type] $uid        [description]
	 * @return [type]             [description]
	 */
	public function getOneData($bonus_name,$uid){
        if($res = $this->where(['uid'=>$uid,'bonus_name'=>$bonus_name])->find()){
        	return $res->toArray();
        }else{
        	return false;
        }
    }

    public function updateOneData($bonus_name,$uid,$total){
        if(!$this->where(['uid'=>$uid,'bonus_name'=>$bonus_name])->find()){
            $this->allowField(true)->save([
                    'uid'=>$uid,
                    'bonus_name'=>$bonus_name,
                    'total'=>$total
                ]);
        }else{
            $this->where(['uid'=>$uid,'bonus_name'=>$bonus_name])->update(['total'=>$total]);
        }
    }

}
